home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / unohelper.py < prev    next >
Text File  |  2009-10-22  |  11KB  |  309 lines

  1. #*************************************************************************
  2. #
  3. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. # Copyright 2008 by Sun Microsystems, Inc.
  5. #
  6. # OpenOffice.org - a multi-platform office productivity suite
  7. #
  8. # $RCSfile: unohelper.py,v $
  9. #
  10. # $Revision: 1.7 $
  11. #
  12. # This file is part of OpenOffice.org.
  13. #
  14. # OpenOffice.org is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Lesser General Public License version 3
  16. # only, as published by the Free Software Foundation.
  17. #
  18. # OpenOffice.org is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU Lesser General Public License version 3 for more details
  22. # (a copy is included in the LICENSE file that accompanied this code).
  23. #
  24. # You should have received a copy of the GNU Lesser General Public License
  25. # version 3 along with OpenOffice.org.  If not, see
  26. # <http://www.openoffice.org/license.html>
  27. # for a copy of the LGPLv3 License.
  28. #
  29. #*************************************************************************
  30. import uno
  31. import pyuno
  32. import os
  33. import sys
  34.  
  35. from com.sun.star.lang import XTypeProvider, XSingleComponentFactory, XServiceInfo
  36. from com.sun.star.uno import RuntimeException, XCurrentContext
  37. from com.sun.star.beans.MethodConcept import ALL as METHOD_CONCEPT_ALL
  38. from com.sun.star.beans.PropertyConcept import ALL as PROPERTY_CONCEPT_ALL
  39.  
  40. from com.sun.star.reflection.ParamMode import \
  41.      IN as PARAM_MODE_IN, \
  42.      OUT as PARAM_MODE_OUT, \
  43.      INOUT as PARAM_MODE_INOUT
  44.  
  45. from com.sun.star.beans.PropertyAttribute import \
  46.      MAYBEVOID as PROP_ATTR_MAYBEVOID, \
  47.      BOUND as PROP_ATTR_BOUND, \
  48.      CONSTRAINED as PROP_ATTR_CONSTRAINED, \
  49.      TRANSIENT as PROP_ATTR_TRANSIENT, \
  50.      READONLY as PROP_ATTR_READONLY, \
  51.      MAYBEAMBIGUOUS as PROP_ATTR_MAYBEAMBIGUOUS, \
  52.      MAYBEDEFAULT as PROP_ATTR_MAYBEDEFAULT, \
  53.      REMOVEABLE as PROP_ATTR_REMOVEABLE
  54.  
  55. def _mode_to_str( mode ):
  56.     ret = "[]"
  57.     if mode == PARAM_MODE_INOUT:
  58.         ret = "[inout]"
  59.     elif mode == PARAM_MODE_OUT:
  60.         ret = "[out]"
  61.     elif mode == PARAM_MODE_IN:
  62.         ret = "[in]"
  63.     return ret
  64.  
  65. def _propertymode_to_str( mode ):
  66.     ret = ""
  67.     if PROP_ATTR_REMOVEABLE & mode:
  68.         ret = ret + "removeable "
  69.     if PROP_ATTR_MAYBEDEFAULT & mode:
  70.         ret = ret + "maybedefault "
  71.     if PROP_ATTR_MAYBEAMBIGUOUS & mode:
  72.         ret = ret + "maybeambigous "
  73.     if PROP_ATTR_READONLY & mode:
  74.         ret = ret + "readonly "
  75.     if PROP_ATTR_TRANSIENT & mode:
  76.         ret = ret + "tranient "
  77.     if PROP_ATTR_CONSTRAINED & mode:
  78.         ret = ret + "constrained "
  79.     if PROP_ATTR_BOUND & mode:
  80.         ret = ret + "bound "
  81.     if PROP_ATTR_MAYBEVOID & mode:
  82.         ret = ret + "maybevoid "
  83.     return ret.rstrip()
  84.     
  85. def inspect( obj , out ):
  86.     if isinstance( obj, uno.Type ) or \
  87.        isinstance( obj, uno.Char ) or \
  88.        isinstance( obj, uno.Bool ) or \
  89.        isinstance( obj, uno.ByteSequence ) or \
  90.        isinstance( obj, uno.Enum ) or \
  91.        isinstance( obj, uno.Any ):
  92.         out.write( str(obj) + "\n")
  93.         return
  94.  
  95.     ctx = uno.getComponentContext()
  96.     introspection = \
  97.          ctx.ServiceManager.createInstanceWithContext( "com.sun.star.beans.Introspection", ctx )
  98.  
  99.     out.write( "Supported services:\n" )
  100.     if hasattr( obj, "getSupportedServiceNames" ):
  101.         names = obj.getSupportedServiceNames()
  102.         for ii in names:
  103.             out.write( "  " + ii + "\n" )
  104.     else:
  105.         out.write( "  unknown\n" )
  106.  
  107.     out.write( "Interfaces:\n" )
  108.     if hasattr( obj, "getTypes" ):
  109.         interfaces = obj.getTypes()
  110.         for ii in interfaces:
  111.             out.write( "  " + ii.typeName + "\n" )
  112.     else:
  113.         out.write( "  unknown\n" )
  114.         
  115.     access = introspection.inspect( obj )
  116.     methods = access.getMethods( METHOD_CONCEPT_ALL )
  117.     out.write( "Methods:\n" )
  118.     for ii in methods:
  119.         out.write( "  " + ii.ReturnType.Name + " " + ii.Name )
  120.         args = ii.ParameterTypes
  121.         infos = ii.ParameterInfos
  122.         out.write( "( " )
  123.         for i in range( 0, len( args ) ):
  124.             if i > 0:
  125.                 out.write( ", " )
  126.             out.write( _mode_to_str( infos[i].aMode ) + " " + args[i].Name + " " + infos[i].aName )
  127.         out.write( " )\n" )
  128.  
  129.     props = access.getProperties( PROPERTY_CONCEPT_ALL )
  130.     out.write ("Properties:\n" )
  131.     for ii in props:
  132.         out.write( "  ("+_propertymode_to_str( ii.Attributes ) + ") "+ii.Type.typeName+" "+ii.Name+ "\n" )
  133.  
  134. def createSingleServiceFactory( clazz, implementationName, serviceNames ):
  135.     return _FactoryHelper_( clazz, implementationName, serviceNames )
  136.  
  137. class _ImplementationHelperEntry:
  138.       def __init__(self, ctor,serviceNames):
  139.       self.ctor = ctor
  140.       self.serviceNames = serviceNames
  141.       
  142. class ImplementationHelper:
  143.       def __init__(self):
  144.       self.impls = {}
  145.       
  146.       def addImplementation( self, ctor, implementationName, serviceNames ):
  147.           self.impls[implementationName] =  _ImplementationHelperEntry(ctor,serviceNames)
  148.       
  149.       def writeRegistryInfo( self, regKey, smgr ):
  150.           for i in self.impls.items():
  151.           keyName = "/"+ i[0] + "/UNO/SERVICES"
  152.           key = regKey.createKey( keyName )
  153.           for serviceName in i[1].serviceNames:
  154.           key.createKey( serviceName )
  155.           return 1
  156.  
  157.       def getComponentFactory( self, implementationName , regKey, smgr ):
  158.       entry = self.impls.get( implementationName, None )
  159.       if entry == None:
  160.          raise RuntimeException( implementationName + " is unknown" , None )
  161.       return createSingleServiceFactory( entry.ctor, implementationName, entry.serviceNames )
  162.  
  163.       def getSupportedServiceNames( self, implementationName ):
  164.       entry = self.impls.get( implementationName, None )
  165.       if entry == None:
  166.          raise RuntimeException( implementationName + " is unknown" , None )
  167.       return entry.serviceNames         
  168.       
  169.       def supportsService( self, implementationName, serviceName ):
  170.       entry = self.impls.get( implementationName,None )
  171.       if entry == None:
  172.          raise RuntimeException( implementationName + " is unknown", None )
  173.           return serviceName in entry.serviceNames         
  174.  
  175.       
  176. class ImplementationEntry:
  177.       def __init__(self, implName, supportedServices, clazz ):
  178.       self.implName = implName
  179.       self.supportedServices = supportedServices
  180.       self.clazz = clazz
  181.  
  182. def writeRegistryInfoHelper( smgr, regKey, seqEntries ):
  183.     for entry in seqEntries:
  184.         keyName = "/"+ entry.implName + "/UNO/SERVICES"
  185.     key = regKey.createKey( keyName )
  186.     for serviceName in entry.supportedServices:
  187.         key.createKey( serviceName )
  188.  
  189. def systemPathToFileUrl( systemPath ):
  190.     "returns a file-url for the given system path"
  191.     return pyuno.systemPathToFileUrl( systemPath )
  192.  
  193. def fileUrlToSystemPath( url ):
  194.     "returns a system path (determined by the system, the python interpreter is running on)"
  195.     return pyuno.fileUrlToSystemPath( url )
  196.  
  197. def absolutize( path, relativeUrl ):
  198.     "returns an absolute file url from the given urls"
  199.     return pyuno.absolutize( path, relativeUrl )
  200.         
  201. def getComponentFactoryHelper( implementationName, smgr, regKey, seqEntries ):
  202.     for x in seqEntries:
  203.     if x.implName == implementationName:
  204.        return createSingleServiceFactory( x.clazz, implementationName, x.supportedServices )
  205.  
  206. def addComponentsToContext( toBeExtendedContext, contextRuntime, componentUrls, loaderName ):
  207.     smgr = contextRuntime.ServiceManager
  208.     loader = smgr.createInstanceWithContext( loaderName, contextRuntime )
  209.     implReg = smgr.createInstanceWithContext( "com.sun.star.registry.ImplementationRegistration",contextRuntime)
  210.  
  211.     isWin = os.name == 'nt' or os.name == 'dos'
  212.     isMac = sys.platform == 'darwin'
  213.     #   create a temporary registry
  214.     for componentUrl in componentUrls:
  215.         reg = smgr.createInstanceWithContext( "com.sun.star.registry.SimpleRegistry", contextRuntime )
  216.     reg.open( "", 0, 1 )
  217.         if not isWin and componentUrl.endswith( ".uno" ):  # still allow platform independent naming
  218.             if isMac:
  219.                componentUrl = componentUrl + ".dylib"
  220.             else:
  221.                componentUrl = componentUrl + ".so"
  222.  
  223.     implReg.registerImplementation( loaderName,componentUrl, reg )
  224.     rootKey = reg.getRootKey()
  225.     implementationKey = rootKey.openKey( "IMPLEMENTATIONS" )
  226.     implNames = implementationKey.getKeyNames()
  227.     extSMGR = toBeExtendedContext.ServiceManager
  228.     for x in implNames:
  229.         fac = loader.activate( max(x.split("/")),"",componentUrl,rootKey)
  230.         extSMGR.insert( fac )
  231.     reg.close()
  232.                 
  233. # never shrinks !
  234. _g_typeTable = {}
  235. def _unohelper_getHandle( self):
  236.    ret = None
  237.    if _g_typeTable.has_key( self.__class__ ):
  238.      ret = _g_typeTable[self.__class__]
  239.    else:
  240.      names = {}
  241.      traverse = list(self.__class__.__bases__)
  242.      while len( traverse ) > 0:
  243.          item = traverse.pop()
  244.          bases = item.__bases__
  245.          if uno.isInterface( item ):
  246.              names[item.__pyunointerface__] = None
  247.          elif len(bases) > 0:
  248.              # the "else if", because we only need the most derived interface
  249.              traverse = traverse + list(bases)#
  250.  
  251.      lst = names.keys()
  252.      types = []
  253.      for x in lst:
  254.          t = uno.getTypeByName( x )
  255.          types.append( t )
  256.          
  257.      ret = tuple(types) , uno.generateUuid()
  258.      _g_typeTable[self.__class__] = ret
  259.    return ret
  260.   
  261. class Base(XTypeProvider):
  262.       def getTypes( self ):
  263.       return _unohelper_getHandle( self )[0]
  264.       def getImplementationId(self):
  265.       return _unohelper_getHandle( self )[1]
  266.  
  267. class CurrentContext(XCurrentContext, Base ):
  268.     """a current context implementation, which first does a lookup in the given
  269.        hashmap and if the key cannot be found, it delegates to the predecessor
  270.        if available
  271.     """
  272.     def __init__( self, oldContext, hashMap ):
  273.         self.hashMap = hashMap
  274.         self.oldContext = oldContext
  275.  
  276.     def getValueByName( self, name ):
  277.         if name in self.hashMap:
  278.             return self.hashMap[name]
  279.         elif self.oldContext != None:
  280.             return self.oldContext.getValueByName( name )
  281.         else:
  282.             return None
  283.         
  284. # -------------------------------------------------
  285. # implementation details
  286. # -------------------------------------------------
  287. class _FactoryHelper_( XSingleComponentFactory, XServiceInfo, Base ):
  288.       def __init__( self, clazz, implementationName, serviceNames ):
  289.       self.clazz = clazz
  290.       self.implementationName = implementationName
  291.       self.serviceNames = serviceNames
  292.       
  293.       def getImplementationName( self ):
  294.       return self.implementationName
  295.  
  296.       def supportsService( self, ServiceName ):
  297.       return ServiceName in self.serviceNames
  298.  
  299.       def getSupportedServiceNames( self ):
  300.       return self.serviceNames
  301.  
  302.       def createInstanceWithContext( self, context ):
  303.       return self.clazz( context )
  304.           
  305.       def createInstanceWithArgumentsAndContext( self, args, context ):
  306.       return self.clazz( context, *args )
  307.       
  308.